package com.udaos.modules.exam.controllers;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import com.udaos.models.wx.Wx_config;
import com.udaos.modules.wx.services.WxConfigService;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

@IocBean
@At("/qrcode")
public class CreateQrcode {

	public static String appId="774f148e627c4ff587e4503b8f55a1bf";
	
	@Inject
	private WxConfigService wxConfigService;
	/**
	 * 首页
	 */
	@At("")
	@Ok("json")
	public void createQrcode(String scene_str) throws Exception{
		WxMpService wxMpService=getWxMpService(appId);
		WxMpQrCodeTicket ticket=wxMpService.getQrcodeService().qrCodeCreateLastTicket(scene_str);
		String url=wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket());
		System.out.println(scene_str+"======>"+url);
		
	}
	/**
	 * 获取微信公众号服务
	 * 
	 * @param wxid
	 * @return
	 */
	public WxMpService getWxMpService(String wxid) {
		WxMpConfigStorage config = getWxConfig(wxConfigService.fetch(wxid));
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(config);
		return wxMpService;
	}
	/**
	 * 获取公众号配置
	 * 
	 * @param appInfo
	 * @return
	 */
	public WxMpConfigStorage getWxConfig(Wx_config appInfo) {
		if (null == appInfo)
			return null;
		WxMpConfigStorage config = new WxMpInMemoryConfigStorage();
		((WxMpInMemoryConfigStorage) config).setAppId(appInfo.getAppid()); // 设置微信公众号的appid
		((WxMpInMemoryConfigStorage) config).setSecret(appInfo.getAppsecret()); // 设置微信公众号的app
		((WxMpInMemoryConfigStorage) config).setPartnerId(appInfo.getPartnerId()); // 设置微信支付商户号
		((WxMpInMemoryConfigStorage) config).setPartnerKey(appInfo.getPartnerKey()); // 设置微信支付商户号
		((WxMpInMemoryConfigStorage) config).setToken(appInfo.getToken()); // 设置微信公众号的token
		((WxMpInMemoryConfigStorage) config).setAesKey(appInfo.getEncodingAESKey()); // 设置微信公众号的EncodingAESKey

		return config;
	}
}
